var request = require("../../utils/request.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {
      avatar: "",
      email: "",
      loginName: "",
      phonenumber: "",
      sex: "",
      userName: "",
    },
    noChange: true,
    imageUrl: null,
    baseUrl: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var that = this
    var baseUrl = wx.getStorageSync('baseUrl')
    var userInfo = wx.getStorageSync('userInfo')
    if (userInfo != null) {
      that.setData({
        userInfo: userInfo,
        imageUrl: baseUrl + userInfo.avatar,
        baseUrl: baseUrl
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '请登录',
        duration: 1000, // 提示窗停留时间，默认1500ms
        showCancel: false
      })
    }
  },

  userLoad: function () {
    var that = this
    var baseUrl = that.data.baseUrl
    wx.getStorage({
      key: 'user',
      success(res) {
        console.log(res)
        // that.setData({
        //   userInfo: res,
        //   imageUrl: baseUrl + userInfo.avatar
        // })
      }
    })
    // if (userInfo != null) {
    //   that.setData({
    //     userInfo: userInfo,
    //     imageUrl: baseUrl + userInfo.avatar
    //   })
    // } else {
    //   wx.showModal({
    //     title: '提示',
    //     content: '请登录',
    //     duration: 1000, // 提示窗停留时间，默认1500ms
    //     showCancel: false
    //   })
    // }
  },

  // 更改昵称
  changeUserName: function (e) {
    var that = this
    var userName = "userInfo.userName"
    var newUserName = e.detail.value
    if (newUserName != '') {
      that.setData({
        [userName]: newUserName
      })
      that.isEqual();
    } else {
      that.setData({
        noChange: true
      })
    }
  },
  //更改邮箱
  changeEmail: function (e) {
    var that = this
    var email = "userInfo.email"
    var newEmail = e.detail.value
    if (newEmail != '') {
      that.setData({
        [email]: newEmail
      })
      that.isEqual();
    } else {
      that.setData({
        noChange: true
      })
    }
  },


  //更改手机
  changePhone: function (e) {
    var that = this
    var phonenumber = "userInfo.phonenumber"
    var newPhonenumber = e.detail.value
    if (newPhonenumber != '') {
      that.setData({
        [phonenumber]: newPhonenumber
      })
      that.isEqual();
    } else {
      that.setData({
        noChange: true
      })
    }
  },


  //校验是否相同或非空
  isEqual: function () {
    var that = this
    var oldUser = wx.getStorageSync('userInfo')
    var newUser = that.data.userInfo
    if (JSON.stringify(oldUser) === JSON.stringify(newUser)) {
      that.setData({
        noChange: true
      })
    } else {
      that.setData({
        noChange: false
      })
    }
  },
  clickAvatar: function () {
    var that = this
    var avatar = "userInfo.avatar"
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        that.setData({
            [avatar]: res.tempFilePaths,
            imageUrl: res.tempFilePaths
          }),
          that.isEqual();
      },
      fail(res) {
        that.isEqual();
        wx.showModal({
          title: '提示',
          content: '请尝试重新上传照片',
          duration: 1000, // 提示窗停留时间，默认1500ms
          showCancel: false,
          confirmColor: "#FF0000"
        })
      }
    })
  },
  //提交表单
  changeMyself: function () {
    var that = this
    var oldUser = wx.getStorageSync('userInfo')
    var oldAvatar = oldUser.avatar
    var avatar = "userInfo.avatar"
    var imgUrl = that.data.userInfo.avatar.toString()
    // 上传图片
    if (imgUrl != '' && oldAvatar.toString() != imgUrl) {
      //  执行图片上传
      request.upload('/public/uploadFile', imgUrl, function (res) {
        console.log(res)
        if (res.code == 200) {
          that.setData({
            [avatar]: res.data
          })
          that.uploadMy()
        }
      })
    } else if (imgUrl != '') {
      that.uploadMy()
    }
  },

  uploadMy: function () {
    var that = this
    request.post("/myself/updateMyself", this.data.userInfo, function (res) {
      if (res.code === 200) {
        request.get("/selectUser", null, function (res) {
          console.log(res)
          if (res.code === 200) {
            wx.setStorageSync('user', res.data)
          }
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '请尝试重新登录',
          duration: 1000, // 提示窗停留时间，默认1500ms
          showCancel: false,
          confirmColor: "#FF0000"
        })
      }
    })
  }
})